<template>
  <el-card>
    <el-form :inline="true">
      <el-form-item label="一级分类">
        <el-select
          :disabled="scene == 0 ? false : true"
          v-model="categoryStore.c1Id"
          placeholder="请选择"
          @change="handler1"
          style="width: 200px"
        >
          <!-- option:label即为显示文字，value属性即为select下拉菜单收集的数据 -->
          <el-option
            v-for="(c1, index) in categoryStore.c1Arr"
            :key="c1.id"
            :label="c1.name"
            :value="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          :disabled="scene == 0 ? false : true"
          v-model="categoryStore.c2Id"
          placeholder="请选择"
          @change="handler2"
          style="width: 200px"
        >
          <el-option
            v-for="(c2, index) in categoryStore.c2Arr"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          :disabled="scene == 0 ? false : true"
          v-model="categoryStore.c3Id"
          placeholder="请选择"
          style="width: 200px"
        >
          <el-option
            v-for="(c3, index) in categoryStore.c3Arr"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, onBeforeUnmount } from 'vue'
import { reqC1 } from '@/api/product/attr'
import useCategoryStore from '@/store/modules/category'

let categoryStore = useCategoryStore()

//路由组件销毁之前,需要把仓库中分类相关的数据清空
onBeforeUnmount(() => {
  //清空仓库的数据
  categoryStore.$reset()
})

const getC1 = () => {
  categoryStore.getC1()
}

onMounted(() => {
  getC1()
})

//一级分类下拉菜单的change事件
const handler1 = () => {
  //需要将2和3级分类清掉
  categoryStore.c2Id = ''
  categoryStore.c3Arr = []
  categoryStore.c3Id = ''
  categoryStore.getC2()
}

//2级分类下拉菜单的change事件
const handler2 = () => {
  //需要将3级分类清掉
  categoryStore.c3Id = ''
  categoryStore.getC3()
}

//接收父组件传递过来的值
defineProps(['scene'])
</script>

<style lang="scss" scoped></style>
